package com.admission.service;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.admission.dao.ArrayInfoDao;
import com.admission.dao.TeacherViewDao;
import com.admission.model.ArrayInfo;
import com.admission.model.TeacherView;
import com.admission.util.DeleteFile;

@Repository
@Transactional
public class TeacherViewManager {
	private TeacherViewDao teacherViewDao;
    private ArrayInfoDao arrayInfoDao;
    
	@Autowired
	public void setTeacherViewDao(TeacherViewDao teacherViewDao) {
		this.teacherViewDao = teacherViewDao;
	}
    @Autowired
	public void setArrayInfoDao(ArrayInfoDao arrayInfoDao) {
		this.arrayInfoDao = arrayInfoDao;
	}

	/**
  	 * 通过id找唯一对象，无返回null
  	 * 
  	 * @param id
  	 * @return
  	 */
  	public TeacherView getTeacherViewById(String id) {
  		return teacherViewDao.findUniqueBy("id", id);
  	}
	/**
	 * 保存、更新
	 * 
	 * @param Entity
	 * @return
	 */
	public void save(TeacherView entity,String photoUrl) throws Exception{
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		if(photoUrl!=null){
			File file = new File(serverrealPath + File.separator
					+ entity.getPhotoUrl());
			DeleteFile.deleteByFile(file);// 删除源文件	
		 entity.setPhotoUrl(photoUrl);
		}
		 teacherViewDao.save(entity);
	}
	/**
	 * 删除
	 * 
	 * @param Entity
	 * @return
	 */
	public void delete(String id) throws Exception{
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		TeacherView entity=this.getTeacherViewById(id);
		File file = new File(serverrealPath + File.separator
					+ entity.getPhotoUrl());
		DeleteFile.deleteByFile(file);// 删除源文件
		teacherViewDao.delete(id);
	}
	
	/**
	 * 根据数据字典小类获取对象
	 * @param type
	 * @return
	 */
	public List<TeacherView> getByType(String type){
		return teacherViewDao.findBy("type", type);
	}
	
	/**
	 * 批量删除
	 * 
	 * @param Entity
	 * @return
	 */
	public void deleteMore(String ids) throws Exception{
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		for(String id : ids.split(",")){
			TeacherView entity=this.getTeacherViewById(id.trim());
			File file = new File(serverrealPath + File.separator
						+ entity.getPhotoUrl());
			DeleteFile.deleteByFile(file);// 删除源文件
			teacherViewDao.delete(id.trim());
		}
	}
	
	/**
	 * 通过filter查询结果
	 * 
	 * @param page
	 * @param filters
	 * @return
	 */
	public Page<TeacherView> getPageByFilter(Page<TeacherView> page,
			List<PropertyFilter> filters){
		return teacherViewDao.findPage(page, filters);
	}

	public Page<TeacherView> getByordTime(final Page<TeacherView> page){
		String hql="from TeacherView a order by type asc,inputTime  desc ";
		return teacherViewDao.findPage(page, hql);
		
	}
	
	
	/**
	 * 改变状态
	 * @param id
	 * @param status
	 */
	public void changeConsult(String status){
		ArrayInfo a=arrayInfoDao.findUniqueBy("itemName",ArrayInfo.TEACHERCONSULT);
		a.setItemValue(status);
		arrayInfoDao.save(a);
	}
	
	/**
	 *判断权限 
	 * @return
	 */
	public String checkConsult(){
		ArrayInfo a=arrayInfoDao.findUniqueBy("itemName",ArrayInfo.TEACHERCONSULT);
		return a.getItemValue();
	}
}
